function getData(id) {
    $.ajax({
        url: $.getEnvUrl()+"/index.php?m=Admin&c=WechatEnt&a=getDeptById&token=ofanap1448334221&id=" + id,
        type: "GET",

        error: function () {
            alert('Error loading XML document');
        },
        success: function (data, status) { //如果调用php成功  
            // var data = JSON.parse(data);
            console.log(data);
            if (data.status == 0) {
                $(".content").removeClass("hidden");
                $(".addPeople").addClass("hidden");
    $(".updatePeople").addClass("hidden");
                //            当该部门为最底层时，去获取人员信息
                  
                $(".table").empty();
                $.ajax({
                    url: $.getEnvUrl()+"/index.php?m=Admin&c=WechatEnt&a=getEmpByDeptId&token=ofanap1448334221&id=" + id,
                    type: "GET",

                    error: function () {
                        console.log('Error loading XML document');
                    },
                    success: function (data, status) { //如果调用php成功  
                        // var data = JSON.parse(data);
                        console.log(data);
                        if (data) {
                            var tableStr = '<tr><th>姓名</th><th>手机号</th><th>职位</th><th><span class="people-block" onclick="addPeople('+id+')">添加</span></th></tr>';
                            for (var i = 0; i < data.length; i++) {
                                tableStr += '<tr><td>' + data[i].name + '</td><td>' + data[i].mobile + '</td><td>'+data[i].position+'</td>';
                             tableStr += '<td><span class="btn btn-default" onclick="updatePeople('+"'"+data[i].userid+"'"+','+id+')">修改</span>';
                                tableStr += '<span class="btn btn-default delete" userid='+"'"+data[i].userid+"'"+'>删除</span></td></tr>'

                            }
                            $(".table").append(tableStr);
                        } else {
                            alert("暂无数据");
                        }

                    }
                });
            } else {
                var str = "<ul>";
                for (var i = 0; i < data.length; i++) {
                    str += '<li class="parent_li id-' + data[i].id + '"><span uid="' + data[i].id + '" title="Collapse this branch" onclick="getSG(' + data[i].id + ');"><i class="icon-calendar"></i>' + data[i].name + '</span></li>';
                }
                str += "</ul>";
                $(".id-" + id).append(str);
            }
           if(id==0){
               getData(1); //当初始加载时，加载2层
           } 
        }
    });
}

getData(0);




function getSG(id) {
    var id = id;
    var ulStr = $(".id-" + id).children("ul"); //查找li下面有没有ul（有没有数据）
    console.log(ulStr);
    console.log(id);
    if (ulStr.length !== 0) {
        if (ulStr.is(":hidden")) {
            ulStr.show();
            console.log("显示");
        } else {
            ulStr.hide();
            console.log("隐藏");
        }
    } else {
        getData(id);
    }
}


